define(["jquery", "easy-admin"], function ($, ea) {
    var attendance_state = 999;
    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'work.attendance/index',
        add_url: 'work.attendance/add',
        edit_url: 'work.attendance/edit',
        delete_url: 'work.attendance/delete',
        export_url: 'work.attendance/export',
        modify_url: 'work.attendance/modify',
    };

    layui.use(function () {
        var laydate = layui.laydate;
        var form = layui.form;
        // 渲染
        laydate.render({
            elem: '#attendance_date'
        });

        form.on('radio(attendance_state)', function (data) {
            var elem = data.elem;
            var value = elem.value; // 获得 radio 值
            attendance_state = value;
            if (value == 1) {//上班
                $("#work_duration_work").removeAttr('disabled');
                $("#work_duration_rest").attr('disabled', true);
                $("#work_duration_leave").attr('disabled', true);

                $("#work_duration_work").prop("checked", true);
                $("#work_duration_rest").prop("checked", false);
                $("#work_duration_leave").prop("checked", false);

                $("#drive_yes").removeAttr('disabled');
                $("#drive_yes").prop("checked", false);

                $("#drive_no").removeAttr('disabled');
                $("#drive_no").prop("checked", false);

            } else if (value == 0) {//休息
                $("#work_duration_rest").removeAttr('disabled');
                $("#work_duration_work").attr('disabled', true);
                $("#work_duration_leave").attr('disabled', true);

                $("#work_duration_rest").prop("checked", true);
                $("#work_duration_work").prop("checked", false);
                $("#work_duration_leave").prop("checked", false);

                $("#drive_yes").attr('disabled', true);
                $("#drive_yes").prop("checked", false);

                $("#drive_no").removeAttr('disabled');
                $("#drive_no").prop("checked", true);
            } else if (value == -1) {//请假
                $("#work_duration_leave").removeAttr('disabled');
                $("#work_duration_work").attr('disabled', true);
                $("#work_duration_rest").removeAttr('disabled');

                $("#work_duration_leave").prop("checked", false);
                $("#work_duration_work").prop("checked", false);
                $("#work_duration_rest").prop("checked", false);

                $("#drive_yes").removeAttr('disabled');
                $("#drive_yes").prop("checked", false);

                $("#drive_no").removeAttr('disabled');
                $("#drive_no").prop("checked", false);
            }
            form.render();
        });

        form.on('radio(work_duration)', function (data) {
            var elem = data.elem;
            var value = elem.value; // 获得 radio 值
            if (attendance_state == -1) {
                if (value == 0) {
                    console.log("请假1天，无法开车")
                    $("#drive_yes").attr('disabled', true);
                    $("#drive_yes").prop("checked", false);

                    $("#drive_no").attr('disabled', true);
                    $("#drive_no").prop("checked", true);
                }else if (value == 0.5) {
                    $("#drive_yes").removeAttr('disabled');
                    $("#drive_yes").prop("checked", false);

                    $("#drive_no").removeAttr('disabled');
                    $("#drive_no").prop("checked", true);
                }
            }

            form.render();
        });
    });

    return {

        index: function () {
            ea.table.render({
                init: init,
                cols: [[
                    { type: 'checkbox' },
                    { field: 'id', title: 'ID', width: 40, search: 'false' },
                    { field: 'date', title: '出勤日期', width: 200, search: 'date-range',timeType:'date'},
                    {
                        field: 'state', title: '状态', width: 100, templet: function (d) {
                            if (d.state === -1) {
                                return '<button type="button" class="layui-btn layui-btn-danger layui-btn-sm">请假</button>';
                            } else if (d.state === 0) {
                                return '<button type="button" class="layui-btn layui-btn-warm layui-btn-sm">休息</button>';
                            }
                            else {
                                return '<button type="button" class="layui-btn layui-btn-sm">上班</button>';
                            }
                        }, selectList: { '-1': '请假', '0': '休息', '1': '上班' }
                    },
                    {
                        field: 'work_duration', title: '工时（天）', width: 100, templet: function (d) {
                            if (d.work_duration > 0) {
                                return d.work_duration;
                            } else {
                                return '---';
                            }
                        }, search: false
                    },
                    {
                        field: 'leave_duration', title: '请假（天）', width: 100, templet: function (d) {
                            if (d.leave_duration > 0) {
                                return d.leave_duration;
                            } else {
                                return '---';
                            }
                        }, search: false
                    },
                    {
                        field: 'drive', title: '驾驶', width: 100, templet: function (d) {
                            if (d.drive === 1) {
                                return '<button type="button" class="layui-btn layui-btn-sm">是</button>';
                            } else {
                                return '<button type="button" class="layui-btn layui-btn-warm layui-btn-sm">否</button>';
                            }
                        }, search: false
                    },
                    { field: 'remark', title: '备注', search: 'false' },
                    { width: 250, title: '操作', templet: ea.table.tool },
                ]],
            });

            ea.listen();
        },
        add: function () {
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
    };
});